import 'package:flutter/material.dart';
import 'package:hugeicons/hugeicons.dart';
import 'package:provider/provider.dart';
import 'package:tool/components/LoginTextFieldColor.dart';

import '../../base/base_widget.dart';
import '../../resources.dart';
import 'vm/table_model.dart';

class TablePage extends StatefulWidget {
  const TablePage({super.key});

  @override
  State<TablePage> createState() => _TablePageState();
}

class _TablePageState extends BaseWidgetState<TableModel, TablePage> {
  @override
  bool get wantKeepAlive => true;

  @override
  TableModel initModel() {
    return TableModel();
  }

  @override
  Widget bodyWidget(BuildContext context) {
    return GestureDetector(
      onTap: () {
        hideKeyboard();
      },
      child: Container(
        width: designWidth.ss,
        height: double.infinity,
        color: Colors.transparent,
        child: Consumer<TableModel>(
            builder: (BuildContext context, model, Widget? child) {
          return Column(
            children: [
              commonTitleBar(word.table_title,
                  titleBackColor: ColorUtils.color_D29000,
                  onTap: model.showAdd
                      ? () {
                          model.closeShowAdd();
                        }
                      : null),
              Expanded(
                child: model.showAdd ? _buildAdd() : _buildList(),
              ),
            ],
          );
        }),
      ),
    );
  }

  Widget _buildAdd() {
    return Container(
      padding: EdgeInsets.all(15.ss),
      child: Column(children: [
        _buildAddRow('课程名称', model.classController, model.classFocusNode,
            must: true),
        _buildAddRow('课程老师', model.teacherController, model.teacherFocusNode),
        _buildAddRow('教室地址', model.roomController, model.roomFocusNode),
        _buildAddRow('备       注', model.markController, model.markFocusNode),
        SizedBox(height: 38.ss),
        Row(
          children: [
            Expanded(
                child: Center(
              child: InkWell(
                onTap: () {
                  hideKeyboard();
                  model.addClear();
                },
                hoverColor: ColorUtils.color_000000.withOpacity(0.3),
                borderRadius: BorderRadius.circular(50.ss),
                child: Container(
                  width: 130.ss,
                  height: 38.ss,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      border: Border.all(color: ColorUtils.color_666666),
                      borderRadius: BorderRadius.circular(50.ss)),
                  child: Text('清空', style: normal616),
                ),
              ),
            )),
            Expanded(
                child: Center(
              child: InkWell(
                onTap: () {
                  hideKeyboard();
                  model.addSave();
                },
                hoverColor: ColorUtils.color_000000.withOpacity(0.3),
                borderRadius: BorderRadius.circular(50.ss),
                child: Container(
                  width: 130.ss,
                  height: 38.ss,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: ColorUtils.color_D29000,
                      borderRadius: BorderRadius.circular(50.ss)),
                  child: Text('保存', style: normalF16),
                ),
              ),
            )),
          ],
        )
      ]),
    );
  }

  Widget _buildAddRow(String title, controller, focusNode,
      {int maxLength = 15, bool must = false}) {
    return Row(
      children: [
        Container(
            width: 5.ss,
            padding: EdgeInsets.only(bottom: 10.ss),
            child: Text(must ? '*' : '', style: normalAD12)),
        Container(
            padding: EdgeInsets.only(bottom: 10.ss),
            child: Text('$title：', style: normal014)),
        Expanded(
          child: TextField(
              maxLines: 1,
              maxLength: maxLength,
              controller: controller,
              focusNode: focusNode,
              keyboardType: TextInputType.text,
              style: normal014,
              decoration: InputDecoration(
                counterStyle: normal612,
                hintText: '请输入',
                hintStyle: normal914,
                constraints: BoxConstraints.tightFor(height: 60.ss),
                border: const UnderlineInputBorder(
                    borderSide: BorderSide(color: ColorUtils.color_333333)),
              )),
        )
      ],
    );
  }

  Widget _buildList() {
    return InteractiveViewer(
      transformationController: model.transformationController,
      maxScale: 2.0,
      minScale: 0.1,
      constrained: false,
      child: Row(
        children: [
          SizedBox(
            width: 40.ss,
            child: Column(
              children: [
                _buildOne('', oneWidth: 40.ss),
                _buildOne('上午',
                    oneWidth: 40.ss,
                    oneHeight: model.oneHeight * model.amCount),
                _buildOne('下午',
                    oneWidth: 40.ss,
                    oneHeight: model.oneHeight * model.pmCount),
                _buildOne('晚上',
                    oneWidth: 40.ss,
                    oneHeight: model.oneHeight * model.nightCount),
              ],
            ),
          ),
          Column(
            children: [
              _buildRow('week', 0),
              for (int column = 0; column < model.amCount; column += 1)
                _buildRow('am${column + 1}', 1 + column),
              for (int column = 0; column < model.pmCount; column += 1)
                _buildRow('pm${column + 1}', 1 + column + model.amCount),
              for (int column = 0; column < model.nightCount; column += 1)
                _buildRow('night${column + 1}',
                    1 + column + model.amCount + model.pmCount),
            ],
          )
        ],
      ),
    );
  }

  Widget _buildRow(String rowKey, int rowIndex) {
    List rowList = model.rowMap[rowKey] ?? ['', '', '', '', '', '', ''];
    if ('week' == rowKey) {
      rowList = model.weeks;
    }
    return Row(
      children: [
        _buildOne(rowIndex < 1 ? '星期' : '第$rowIndex节'),
        for (int column = 0; column < model.weekCount; column += 1)
          _buildOne(rowList[column], onTap: () {
            model.addClass(rowKey, column);
          }),
      ],
    );
  }

  Widget _buildOne(String content,
      {double? oneWidth, double? oneHeight, VoidCallback? onTap}) {
    oneWidth ??= model.oneWidth;
    oneHeight ??= model.oneHeight;
    return InkWell(
      onTap: onTap,
      child: Container(
        width: oneWidth,
        height: oneHeight,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: ColorUtils.color_D29000.withOpacity(0.1),
          border: Border.all(
              color: ColorUtils.color_333333.withOpacity(0.1), width: 0.5),
        ),
        child: content.isEmpty && onTap != null
            ? const HugeIcon(
                icon: HugeIcons.strokeRoundedAdd01,
                color: ColorUtils.color_999999,
                size: 12,
              )
            : Text(
                content,
                style: normal012,
              ),
      ),
    );
  }
}
